package com.flea.hclv.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.constant.Const;
import com.flea.hclv.model.entity.HGuide;
import com.flea.hclv.model.entity.HGuideTravelGroup;
import com.flea.hclv.model.entity.vo.GuideTravelGroupQueryVo;
import com.flea.hclv.model.entity.vo.HGuideTravelGroupVo;
import com.flea.hclv.service.HGuideService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import com.flea.system.platform.session.FleaOrgUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: project
 * @description: 导游管理
 * @author: shiliqiang
 * @create: 2018-11-13 14:43
 * @version: 1.0
 */
@RequestMapping("/flea/hclv/guide/")
@Controller
public class HGuideController {

    @Autowired
    private HGuideService hGuideService;

    /**
     * 添加需要审核的信息
     *
     * @param request request
     * @param model   需要审核的信息
     * @return String
     */
    @ResponseBody
    @RequestMapping("submitGuideInfo")
    public ServerResponse<String> submitGuideInfo(HttpServletRequest request, String model) {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        return hGuideService.saveGuideInfo(userId, model);
    }
    /**
     * 更新需要审核的信息
     *
     * @param request request
     * @param model   需要审核的信息
     * @return String
     */
    @ResponseBody
    @RequestMapping("updateGuideInfo")
    public ServerResponse<String> updateGuideInfo(HttpServletRequest request, String model) {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        return hGuideService.updateGuideInfo(userId, model);
    }

    /**
     * 获取所有已经审核的导游列表
     * @param paging 分页
     * @param queryName  姓名
     * @param queryCertificatesNumber  身份证号
     * @param queryProvince 常在地
     * @param queryCity 常在地
     * @return list
     */
    @ResponseBody
    @RequestMapping("selectAll")
    public ServerResponse<FleaPage<HGuide>> selectAll(FleaPage<HGuide> paging,
                                                      @RequestParam(value = "queryName", required = false) String queryName,
                                                      @RequestParam(value = "queryCertificatesNumber", required = false) String queryCertificatesNumber,
                                                      @RequestParam(value = "queryProvince", required = false) String queryProvince,
                                                      @RequestParam(value = "queryCity", required = false) String queryCity) {
        //查询条件 导游名字  身份证号 导游信息审核状态 省 市
        String queryAuditStatus = Const.GuideAuditStatusEnum.PASSED.getCode();
        return hGuideService.selectAll(paging.getCurPage(), paging.getPageSize(), queryName,
                queryCertificatesNumber, queryAuditStatus, queryProvince, queryCity);
    }

    /**
     * 获取未审核的导游列表
     * @param paging 分页
     * @return list
     */
    @ResponseBody
    @RequestMapping("selectAllUnaudited")
    public ServerResponse<FleaPage<HGuide>> selectUnaudited(FleaPage<HGuide> paging) {
        String un_auditCode  = Const.GuideAuditStatusEnum.UN_AUDIT.getCode();
        return hGuideService.selectUnaudited(paging.getCurPage(),paging.getPageSize(),un_auditCode);
    }






    @ResponseBody
    @RequestMapping("getOwnerInfo")
    public ServerResponse<HGuide> getGuideInfo(HttpServletRequest request) {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        return hGuideService.getGuideInfo(null,userId);
    }

    /**
     * 查看需要审核的导游详细信息
     * @param guideId 导游ID
     * @return MAP
     */
    @ResponseBody
    @RequestMapping("getGuideInfo")
    public ServerResponse<HGuide> getGuideInfo(String guideId) {
        return hGuideService.getGuideInfo(guideId,null);
    }

    @ResponseBody
    @RequestMapping("getGuideInfoByUserId")
    public ServerResponse<HGuide> getGuideInfoByUserId(HttpServletRequest request) {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        return hGuideService.getGuideInfoByUserId(userId);
    }

//    @ResponseBody
//    @RequestMapping("checkGuideInfoByUserId")
//    public ServerResponse<HGuide> checkGuideInfoByUserId(HttpServletRequest request) {
//        String userId = FleaOrgUtils.getCurUserUuid(request);
//        return hGuideService.getGuideInfoByUserId(userId);
//    }




    /**
     * 更新审核状态
     * @param request request
     * @param
     * @return string
     */
    @ResponseBody
    @RequestMapping("updateAuditStatus")
    public ServerResponse<String> updateAuditStatus(HttpServletRequest request,String guideId,
                                @RequestParam(value = "notPassCause",required = false)String notPassCause) {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        return hGuideService.updateAuditStatus(userId,guideId,notPassCause);
    }

    //管理员操作导游状态
    @ResponseBody
    @RequestMapping("operStatus")
    public ServerResponse<String> updateOperateStatus(HttpServletRequest request,String guideId,
                                                    String status) {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        return hGuideService.updateOperateStatus(userId,guideId,status);
    }

    /**
     * 更新状态
     * @param request request
     * @param status 导游自己的状态
     * @return string
     */
    @ResponseBody
    @RequestMapping("updateStatusByUserId")
    public ServerResponse<String> updateStatus(HttpServletRequest request, String status) {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        return hGuideService.updateStatusByUserId(userId,status);
    }

    /**
     * 指派导游
     * @param request session
     * @param groupId  团ID
     * @param guideId  导游ID
     * @return String
     */
    @ResponseBody
    @RequestMapping("assignGuide")
    public ServerResponse<String> receiveGroup(HttpServletRequest request, String groupId, String guideId,String deptId,Float guideCost) throws Exception {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        return hGuideService.saveGuideGroup(userId,groupId,guideId,deptId,guideCost);
    }

    /**
     * 取消指派
     * @param travelGroupId  团ID
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("cancelAssignGuide")
    public ServerResponse<String> cancelAssignGuide(String travelGroupId) throws Exception {
        return hGuideService.deleteGuideGroupRelation(travelGroupId,null);
    }



    /**
     * 给导游付钱
     * @param request session
     * @param model model
     * @return string
     */
    @ResponseBody
    @RequestMapping("payForGuide")
    public ServerResponse<String> payForGuide(HttpServletRequest request, String model) {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        HGuideTravelGroup hGuideTravelGroup = JSONObject.parseObject(model,HGuideTravelGroup.class);

        return hGuideService.saveGuideGroupPayInfo(userId,hGuideTravelGroup);
    }

    /**
     * 导游接团记录
     * @param request session
     * @param paging 分页
     * @return list
     */
    @ResponseBody
    @RequestMapping("guideTravelGroupPayInfo")
    public ServerResponse<FleaPage<HGuideTravelGroup>> getGuideTravelGroupPayInfo(HttpServletRequest request, FleaPage<List<HGuideTravelGroupVo>> paging,
                                    String model){
//                                    , @RequestParam(value = "groupStatus",required = false) String groupStatus,
//                                                                                  @RequestParam(value = "groupName",required = false)String groupName,
//                                                                                  @RequestParam(value = "guideName",required = false)String guideName,
//                                                                                  @RequestParam(value = "guidePhone",required = false)String guidePhone) {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        //status 支付状态   团名称  导游姓名  导游电话
        //model: {"groupStatus":"D_GUIDE_PAY_STATUS_YES","groupName":"123","guideName":"123","guidePhone":"123"}
        GuideTravelGroupQueryVo queryVo = JSON.parseObject(model, GuideTravelGroupQueryVo.class);
        if (queryVo==null){
            queryVo = new GuideTravelGroupQueryVo();
        }
        String groupStatus = queryVo.getGroupStatus();
        String groupName = queryVo.getGroupName();
        String guideName = queryVo.getGuideName();
        String guidePhone = queryVo.getGuidePhone();
        ServerResponse<FleaPage<HGuideTravelGroup>> guideTravelGroupPayInfo = hGuideService.getGuideTravelGroupPayInfo(userId, paging.getCurPage(), paging.getPageSize(), groupStatus, groupName, guideName, guidePhone);
        return guideTravelGroupPayInfo;
    }





    /**
     * 跳转
     * @return  ModelAndView
     */
    @RequestMapping("toRegistr")
    public ModelAndView toRegistr(){
        return new ModelAndView("redirect:/static/app/register/guide.jsp");
    }
    
    /**
     * 跳转
     * @return  ModelAndView
     */
    @RequestMapping("toLogin")
    public ModelAndView toLogin(){
        return new ModelAndView("redirect:/static/app/register/login.jsp");
    }
    
    /**
     * 跳转
     * @return  ModelAndView
     */
    @RequestMapping("toFindPwd")
    public ModelAndView toFindPwd(){
        return new ModelAndView("redirect:/static/app/register/findPwd.jsp");
    }
}
